from typing import Optional
from com.linkstec.dao.base.base_dao import BaseDao
from com.linkstec.models.user import User

class UserDao(BaseDao):
    def find_all(self) -> list[User]:
        query = "SELECT * FROM users"
        return [User(**row) for row in self.execute_query(query)]

    def find_by_id(self, user_id: int) -> Optional[User]:
        query = "SELECT * FROM users WHERE id = %s"
        result = self.execute_query(query, (user_id,))
        return User(**result[0]) if result else None

    def create(self, user: User) -> int:
        query = """INSERT INTO users (username, email) 
                   VALUES (%s, %s)"""
        return self.execute_update(query, (user.username, user.email))

    def update(self, user: User) -> int:
        query = """UPDATE users 
                   SET username = %s, email = %s 
                   WHERE id = %s"""
        return self.execute_update(query,
            (user.username, user.email, user.id))

    def delete(self, user_id: int) -> int:
        query = "DELETE FROM users WHERE id = %s"
        return self.execute_update(query, (user_id,))